// /*eslint-disable */
// import jianliMenuList from '!json-loader!./jianli.json';
// import xiangmubuMenuList from '!json-loader!./xiangmubu.json';
// import zhihuibuMenuList from '!json-loader!./zhihuibu.json';
// /*eslint-enable */

/*eslint-disable */
import storageApi from '../../core/storage/storage';

@Inject('$log', '$rootScope', '$http', '$stateParams', '$state', '$interval', 'ngDialog')
class IndexController {
  constructor() {
    let _this = this;
    this.userName = $rootScope.userName || storageApi.get('userName');
    $rootScope.orgType = $rootScope.orgType || storageApi.get('orgType');
    this.$getMsgNum();

    $rootScope.msgInterval = $interval(function () {
      _this.$getMsgNum();
    }, 30000);
  }

  logout() {
    this
      .$http
      .get('/api/v1/logout')
      .then((res) => {
        if (res.success) {
          this.$state.go('login');
          this.$rootScope.isLogined = false;
          this.$interval.cancel(this.$rootScope.msgInterval);
        }
      });
  }

  changePwd() {
    this.ngDialog.open({
      template: 'pwdDialog',
      className: 'ngdialog-theme-default pa-confirm-theme pa-confirm-self systemManage-add-dialog',
      controllerAs: 'vm',
      controller($rootScope, $http, $scope, $log, $state, loginService, ngDialogService) {
        'ngInject';

        this.userInfo = {
          loginName: $rootScope.userName || storageApi.get('userName'),
        };

        this.submit = (param) => {
          // 修改表单是否提交过状态
          param.$submitted = true;
          // 表单校验
          if (param.$invalid) {
            return false;
          } else if (param.confirmPassWord.$viewValue !== param.newPassWord.$viewValue) {
            ngDialogService.alertError('确认密码和新密码不一致');
            return false;
          } else {
            loginService.update(this.userInfo)
              .then((res) => {
                if (res.success) {
                  ngDialogService.alertSuccess(res.message);
                  $scope.closeThisDialog();
                  $state.go('login');
                } else {
                  ngDialogService.alertError(res.message);
                }
              });
          }
        };
      },
    });
  }

  goMessage() {
    this.$rootScope.currentIndex = 1;
    this.$state.go('message');
  }

  // APP下载
  downloadApp() {
    this.$rootScope.currentIndex = 2;
    this.ngDialog.open({
      template: 'codeDialog',
      className: 'ngdialog-theme-default pa-code-dialog',
      controllerAs: 'vm',
      controller($scope) {
        'ngInject';

        // 下载用户手册
        this.getDocx = () => {
          return `/sgms/api/v1/common/downLoad/平安工程APP用户手册.docx`;
        }

        // 获取二维码图片 平安
        this.getCodeImg = () => {
          let localHost = location.host;
          // let qrContent = encodeURIComponent(`http://101.37.24.111:8080/uploadfile/apk/sgms.apk`);
          let qrContent = encodeURIComponent(`http://114.215.209.60:8780/uploadfile/apk/sgms.apk`);
          let requestUrlStr =
          `/sgms/api/v1/common/qr?size=160&code=${qrContent}`;
          return requestUrlStr;
        }
      },
    });
  }

  isActive() {
    this.$rootScope.currentIndex = 0;
  }

  isDown() {
    this.isDropDown = !this.isDropDown;
  }

  // 获取消息未读数量
  $getMsgNum() {
    this
      .$http
      .get(`/api/v1/message/getUnReadNum`)
      .then((res) => {
        if (res.success) {
          this.msgCount = res.data;
        } else {
          this.msgCount = 0;
        }

      })
  }
}

export default IndexController;
